package com.example.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.Orders;
import com.example.elmspringboot.service.Impl.OrderServiceImpl;
import com.example.elmspringboot.service.OrderService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/orders")
public class OrdersController {
    @Resource
    private OrderService orderService;
// 保存订单了，同一家店的同一个人只能有一个订单
    @PostMapping
    public Result saveOrder(@RequestBody Orders order) {
        return new Result(true, orderService.insertDetails(order));
    }
// 获得用户的所有订单
    @GetMapping("/get-user-order")
    public Result getOrders(@RequestParam Integer userId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(String.valueOf(userId) != null, Orders::getUserId, userId);
        return new Result(true, orderService.list(wrapper), "success");
    }
//    通过用户名与商店名获得未支付的订单
    @GetMapping("/get-order")
    public Result getOrder(@RequestParam Integer userId, @RequestParam Integer shopId) {
        System.out.println(userId);
        System.out.println(shopId);
        Orders one = orderService.getOne(new QueryWrapper<Orders>().eq("order_state", 0)
                .eq("user_id", userId).eq("shop_id", shopId));
        return new Result(true, one);
    }
// 将订单改为已支付
    @PostMapping("/update-order")
    public Result updateOrder(@RequestParam Integer id){
        Orders byId = orderService.getById(id);
        byId.setOrderState(1);
        orderService.updateById(byId);
        return new Result(true);
    }
//    删除订单
    @DeleteMapping("/remove-order")
    public Result removeOrder(@RequestParam Integer id){
        return new Result(true, orderService.removeById(id));
    }

//  商家看到所有的订单
    @GetMapping("/get-order-business")
    public Result getOrderBusiness(@RequestParam Integer shopId){
        return new Result(true,orderService.getBusinessOrders(shopId));
    }
    // 更改订单商家确认状态
    @PutMapping("/update-status")
    public Result updateStatus(@RequestBody Orders orders){
        return new Result(true, orderService.updateById(orders));
    }

}
